#include "dependency.h"
#include "hook.h"
#include "module.h"
MODULE_LICENSE("GPL");
MODULE_AUTHOR("lihan");

/// @brief NF HOOK
static struct nf_hook_ops nfop_in={
	.hook = hook_main, 				// hook函数
	.pf = PF_INET,					// hook协议族
	.hooknum = NF_INET_PRE_ROUTING,	// hook点
	.priority = NF_IP_PRI_FIRST		// hook优先级 (nf_ip_hook_priorities)
};

static struct nf_hook_ops nfop_out={
	.hook = hook_main,
	.pf = PF_INET,
	.hooknum = NF_INET_POST_ROUTING,
	.priority = NF_IP_PRI_FIRST
};

static struct nf_hook_ops natop_in={
	.hook = hook_nat_in,
	.pf = PF_INET,
	.hooknum = NF_INET_PRE_ROUTING,
	.priority = NF_IP_PRI_NAT_DST	// 目的NAT
	// 公网主机发出数据包到达NAT设备时将报文中公网的源IP地址NAT转换为私网IP地址
};

static struct nf_hook_ops natop_out={
	.hook = hook_nat_out,
	.pf = PF_INET,
	.hooknum = NF_INET_POST_ROUTING,
	.priority = NF_IP_PRI_NAT_SRC	// 源NAT
	// 从私网主机发出数据包时将报文中私网的源IP地址NAT转换为公网IP地址
};

static int mod_init(void){
	printk("firewall module loaded.\n");
	nf_register_net_hook(&init_net,&nfop_in);
	nf_register_net_hook(&init_net,&nfop_out);
	nf_register_net_hook(&init_net,&natop_in);
	nf_register_net_hook(&init_net,&natop_out);
	netlink_init();
	conn_init();
	return 0;
}

static void mod_exit(void){
	printk("firewall module exit.\n");
	nf_unregister_net_hook(&init_net,&nfop_in);
	nf_unregister_net_hook(&init_net,&nfop_out);
	nf_unregister_net_hook(&init_net,&natop_in);
	nf_unregister_net_hook(&init_net,&natop_out);
	netlink_release();
	conn_exit();
}


module_init(mod_init);
module_exit(mod_exit);
